namespace CRM_Perf_BenchMark
{
    using System;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using System.Net;
    using System.Text;
    using System.Xml;    

	/// <summary>
	/// 1. Starts at Sales homepage
	/// 2. Click on Accounts
	/// 3. Add random search of two character string and click find
	/// 4. Double click an account
	/// 5. Click on Activities within the account page
	/// </summary>

    public class AccountActivityRollup : CrmWebTest
    {
        public void AccountActivityRollup_PreWebTest(object sender, PreWebTestEventArgs e)
        {
			EntityRequest er = new EntityRequest();
			er.Type = EntityNames.Users;
			er.ReturnAs = EntityNames.Users;
			er.ParentID = EntityManager.Instance.OrgId;

            System.Collections.Hashtable userProps = new System.Collections.Hashtable();
            if (e.WebTest.Context.ContainsKey("UserRole"))
            {
                userProps.Add("role", e.WebTest.Context["UserRole"]);
            }            

			EntityRequest AccountEr = new EntityRequest();
			AccountEr.Type = EntityNames.Accounts;
			AccountEr.ParentID = Guid.NewGuid();
			AccountEr.ReturnAs = EntityNames.Accounts;

			userProps.Add(EntityNames.Accounts, new EntityRequest[] { AccountEr });

            er.Props = userProps;

			System.Collections.Hashtable Entities = EntityManager.Instance.GetEntities(er);

			user = (CRMEntity)Entities[EntityNames.Users];
			account = (CRMEntity)Entities[EntityNames.Accounts];

                        
            e.WebTest.UserName = user["domainName"];
            e.WebTest.Password = user["userpassword"];
            
            crmServer = user["CrmServer"];
            tokenKey = EntityManager.Instance.ReadTokenKey(user["organizationid"]);           
        }

        public void AccountActivityRollup_PostWebTest(object sender, PostWebTestEventArgs e)
        {            
            EntityManager.Instance.FreeEntity(user);
            EntityManager.Instance.FreeEntity(account);
        }

        public AccountActivityRollup()
        {
            WebRequest.RegisterPrefix("crm", new crmRequestFactory());
            string random = Utils.GetRandomString(2, 2);
            gridXml = GridXmlHelper.AccountSearchView(-1, searchAccountView, random, null);

            this.Proxy = null;            
            PreWebTest += new EventHandler<PreWebTestEventArgs>(AccountActivityRollup_PreWebTest);
            PostWebTest += new EventHandler<PostWebTestEventArgs>(AccountActivityRollup_PostWebTest);
        }

        static AccountActivityRollup()
        {          
        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
			this.BeginTransaction("NavAccountHomepage");

            /// Get the home page
            _root_homepage_aspx hp = new _root_homepage_aspx(user);
			hp.etc = 1;
			yield return (hp);            

			this.EndTransaction("NavAccountHomepage");

			this.BeginTransaction("SearchAccounts");

            // Search for the account name
            AppGridWebService.AppGridWebService ag = new AppGridWebService.AppGridWebService();
            WebTestRequest wtr = null;

            try { ag.Reset(gridXml, gridId); }
            catch (crmException x)
            {
                wtr = (CrmRequest)x.wtr;
                wtr.Url = Utils.ReplaceServerName(wtr.Url, user);
            }
            yield return (wtr);

            this.EndTransaction("SearchAccounts");

            this.BeginTransaction("OpenAccount");

            sfa_accts_edit_aspx dp = new sfa_accts_edit_aspx(user);
            dp.id = account["accountid"];
            yield return (dp);

            string referer = dp.lastResponse.ResponseUri.ToString();

            _forms_FormScript_js_aspx fs = new _forms_FormScript_js_aspx(user);
            fs.etc = 1;
            fs.FormType = "main";
            fs.FormName = "crmForm";
            WebTestRequestHeader headerItem = new WebTestRequestHeader();
            headerItem.Name = "referer";
            headerItem.Value = referer;
            fs.Headers.Add(headerItem);
            yield return (fs);
                        
            this.EndTransaction("OpenAccount");

            this.BeginTransaction("AccountActivityRollup");

            sfa_accts_areas_aspx acctAreas = new sfa_accts_areas_aspx(user);
            acctAreas.oId = new Guid(account["accountid"]);
            acctAreas.oType = 1;
            acctAreas.security = 852023;
            acctAreas.tabSet = "areaActivities";
            yield return (acctAreas);

            this.EndTransaction("AccountActivityRollup");
        }

        private string gridXml;
        private string gridId = "crmGrid";
        private string searchAccountView = "2D1187C4-23FE-4BB5-9647-43BB1C6DDBD1";

        private CRMEntity user;
        private CRMEntity account;
        private string crmServer;
        private string tokenKey;
    }
}